package top.boycharse.blog.controller.manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import top.boycharse.blog.bean.Response.*;
import top.boycharse.blog.bean.request.ArticleInfo;
import top.boycharse.blog.dao.bean.Article;
import top.boycharse.blog.dao.bean.Label;
import top.boycharse.blog.dao.bean.User;
import top.boycharse.blog.dao.service.ArticleService;
import top.boycharse.blog.dao.service.LabelService;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/manager/article")
public class ManagerArticleController {

    public static final String LABEL_NOT_EXIT = "标签不存在！";

    public static final String ARTICLE_NOT_EXIT = "文章不存在！";

    @Autowired
    private ArticleService articleService;

    @Autowired
    private LabelService labelService;

    @PostMapping("/uploadArticle")
    public BaseResponse<?> uploadArticle(@RequestBody ArticleInfo info) {
        if (StringUtils.isEmpty(info.getArticleName())) {
            return BaseResponse.buildParamException();
        }

        List<Integer> labelIdList = info.getLabels();
        if (labelIdList != null) {
            for (Integer temp : labelIdList) {
                Label label = labelService.findByLabelId(temp);
                if (label == null) {
                    return BaseResponse.buildFailMsg(ResponseCode.CODE_FAIL,LABEL_NOT_EXIT);
                }
            }
        }


        Article article = new Article(info);
        String username = SecurityContextHolder.getContext().getAuthentication().getName();

        // 创建文章作者
        User user = new User(username);
        article.setAuthor(user);
        articleService.saveArticle(article);

        return BaseResponse.buildSuccessMsg(ResponseMsg.MSG_SUCCESS);
    }

    @DeleteMapping("/deleteArticle")
    public BaseResponse<?> deleteArticle(@RequestParam("id")Integer id) {
        Article article = articleService.findArticleById(id);
        if (article == null) {
            return BaseResponse.buildFailMsg(ResponseCode.CODE_FAIL,ARTICLE_NOT_EXIT);
        }
        articleService.deleteArticle(article);
        return BaseResponse.buildSuccessMsg(ResponseMsg.MSG_SUCCESS);
    }

    @GetMapping("/getArticleList")
    public BaseResponse<?> getArticleList(@RequestParam("pageIndex") Integer pageIndex) {
        Page<Article> articlePages = articleService.getArticleWithPage(pageIndex);
        List<Article> articles = articlePages.getContent();
        List<ArticleRespone> articleRespones = new LinkedList<>();
        for (Article temp: articles) {
            articleRespones.add(new ArticleRespone(temp));
        }
        PageResponse<List<ArticleRespone>> response = new PageResponse<>(pageIndex,
                articlePages.getNumberOfElements(),articlePages.getTotalPages(),articleRespones);

        return new BaseResponse<>(response);
    }
}
